<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/5
 * Time: 9:52
 */

namespace app\admin\controller;

use app\admin\model\Role as RoleModel;
use app\admin\model\Node as NodeModel;
use think\Collection;
use think\Request;
use think\Db;

class RoleController extends BaseController
{
    public function lst()
    {
        $data = RoleModel::order('create_time','asc')->paginate(8);
        $page = $data->render();
        $this->assign([
            'page' => $page,
            'data' => $data,
        ]);
        return $this->fetch();
    }

    public function add()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $model = new RoleModel();
                if($model->validate('RoleValidate')->allowField(true)->save($params)){
                    $this->success('添加角色信息成功!',url('admin/role/lst'),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    public function edi()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                $id = $request->param('id');
                $this->assign('data',RoleModel::find($id));
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $model = new RoleModel();
                if($model->validate('NodeValidate')->allowField(true)->isUpdate(true)->save($params)){
                    $this->success('修改角色信息成功!',url('admin/role/lst'),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    public function del()
    {
        $id = $this->request->param("id");
        if(RoleModel::destroy($id)){
            return true;
        }
        return false;
    }

    public function showNode()
    {
        $params = $this->request->param();
        $nodeAll = (new Collection(NodeModel::where('status','eq',1)->select()))->toArray();
        $roleInfo = RoleModel::get($params['id']);
        $roleNode = (new Collection($roleInfo->nodes))->toArray();

        foreach($nodeAll as $k=>$v){
            foreach($roleNode as $key=>$val){
                if($v['id'] == $val['id']){
                    $nodeAll[$k] = $roleNode[$key];
                }
            }
        }
        $nodeMake = [];
        foreach($nodeAll as $a=>$b){
            $nodeMake[$b['controller']][] = $b;
        }

        $this->assign([
            'node' => $nodeMake,
            'roleId' => $params['id'],
        ]);
        return $this->fetch();
    }

    public function bindNode()
    {
        $params = $this->request->param();
        if(empty($params['role_id'])){
            $this->error('绑定节点所需的参数错误!');
        }

        Db::table('role_node')->where('role_id','eq',$params['role_id'])->delete();
        if(empty($params['node_id'])){
            $this->success('绑定权限成功',url('admin/role/lst'),'',1);
        }

        $addNodeList = array_values($params['node_id']);
        $roleInfo = RoleModel::get($params['role_id']);
        $res = $roleInfo->nodes()->saveAll($addNodeList);
        if($res){
            $this->success('绑定权限成功',url('admin/role/lst'),'',1);
        }
        else{
            $this->error('权限绑定失败');
        }
    }
}